<?php
class XVersion 
{
	private $m_software;  //VERSION
	private $m_os;     
	private $m_qt;   // Qt.conf
	private $m_ip;   // udpd.conf
	private $m_gateway; // udpd.conf
	private $m_udpPort; // udpd.conf
	private $m_mac;  // udpd.conf
	private $m_webVersion; // /usr/www/version
	
	function __construct()
	{
		$this->readVERSION();
		$this->readUdpdConf();
		$this->readQtConf();
		$this->readOS();
		$this->readWebVersion();
	}
	
	public function getSoftware() { return $this->m_software; }
	public function getQt() { return $this->m_qt;}
	public function getOs() { return $this->m_os;}
	public function getIp() { return $this->m_ip; }  //udpd.conf
	public function getGateway() {return $this->m_gateway;}
	public function getUdpPort() {return $this->m_udpPort;}
	public function getMac() {return $this->m_mac;}
	public function getWebVersion() {return $this->m_webVersion;}

	private function readWebVersion()
	{
		$file ="/usr/www/WebVersion";
		if (false === ($fd = fopen($file, 'r'))) {
			die("[NG] Open WebVersion Failed\r");
            $this->m_webVersion="Cannot Find WebVersion";
            return;
		} 
		$this->m_webVersion = trim(fgets($fd));
		fclose($fd);
	}

	private function readVERSION () 
	{
		$file ="/usr/infomat/VERSION";
		if (false === ($fd = fopen($file, 'r'))) {
			die("[NG] Open VERSION Failed\r");
            $this->m_software="Cannot Find VERSION";
            return;
		} 

		$tmpMajor = trim(fgets($fd));
		$major = str_replace("MAJOR=","", $tmpMajor);
		$tmpMinor = trim(fgets($fd));
		$minor = str_replace("MINOR=","", $tmpMinor);
		$tmpRelease = trim(fgets($fd));
		$release = str_replace("RELEASE=","", $tmpRelease);
		$tmpBuild = trim(fgets($fd));
		$build = str_replace("BUILD=","", $tmpBuild);

		$this->m_software = $major.".".$minor.".".$release."-".$build;
        if (empty($this->m_software)) {
            $this->m_software = "[NG] Cannot Get Software Version";
        }
    
		fclose($fd);
	}
	
	private function readUdpdConf ()
	{
		$file ="/usr/infomat/udpd.conf";
		if (false === ($fd = fopen($file, 'r'))) {
			die("[NG] Open udpd.conf\r");
            $this->m_ip = $this->m_udpPort = $this->mac = "[NG] Open udpd.conf Failed";
            return;
		} 
		$tmpIp = trim(fgets($fd));
		$this->m_ip = str_replace("IPV4ADDRESS=","", $tmpIp);
		
		$tmpGateway = trim(fgets($fd));
		$this->m_gateway = str_replace("GATEWAY=","", $tmpGateway);
		
		$tmpUdpPort = trim(fgets($fd));
		$this->m_udpPort = str_replace("UDPPORT=","", $tmpUdpPort);
		
		$nothing = fgets($fd);
		$nothing = fgets($fd);
		
		$tmpMac = trim(fgets($fd));
		$this->m_mac = str_replace("MACADDR=","", $tmpMac);
		
        if (empty($this->m_ip)) {
            $this->m_ip = "Cannot Get Ip Address";
        }
        
        if (empty($this->m_gateway)) {
            $this->m_gateway = "Cannot Get Gateway";
        }
        
        if (empty($this->m_mac)) {
            $this->m_mac = "Cannot Get Mac Address" ;
        }
		fclose($fd);
	}
	
	private function readOS()
	{
		//exec("/bin/uname -s",$name);
		//exec("/bin/uname -r", $ver);
		$ver = exec("/bin/uname -a");
		$this->m_os = $ver;
        if (empty($this->m_os)) {
            $this->m_os = "Cannot Get OS Version";
        }
	}
	
	private function readQtConf()
	{
		$file ="/etc/Qt.conf";
		if (false === ($fd = fopen($file, 'r'))) {
			die("[NG] Open qt.conf\r");
            $this->m_qt = "[NG] Open Qt.conf Failed";
            return;
		} 
		fgets($fd);
		$tmpQt = trim(fgets($fd));
		$this->m_qt = str_replace("Ver=","", $tmpQt);
        
        if (empty($this->m_qt)) {
            $this->m_qt = "Cannot Get Qt Version";
        }
        
		fclose($fd);
	}
}
//$xVersion = new XVersion;
//$software = $xVersion->getSoftware();
//echo $software;
?>
